// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2022 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

using Microsoft.Extensions.Configuration;
using System.Reflection;

namespace Abc.Core;

/// <summary>
/// SqlSugar 默认设置
/// </summary>
public class SqlSugarDbSettingOptions : IConfigurableOptionsListener<SqlSugarDbSettingOptions>
{
    /// <summary>
    /// 雪花ID
    /// </summary>
    public int SnowFlakeSingle_WorkId { get; set; } = 1;

    /// <summary>
    /// 实体数据库是否自增
    /// </summary>
    public bool EntityIsIdentity { get; set; } = true;

    /// <summary>
    /// 数据库名称
    /// </summary>
    public string DataBaseName { get; set; }

    /// <summary>
    /// 数据库创建路径
    /// </summary>
    public string DatabaseDirectory { get; set; }

    /// <summary>
    /// 是否初始化数据库
    /// </summary>
    public bool EnableInitDatabase { get; set; }

    ///// <summary>
    ///// 实体命名空间前缀,数组，可多个
    ///// </summary>
    //public List<string> EntitiesNamespacePrefixs { get; set; }

    ///// <summary>
    ///// 实体所在程序集，可配置多个
    ///// </summary>
    //public List<string> EntitiesAssemblys { get; set; }

    /// <summary>
    /// 是否记录执行的sql
    /// </summary>
    public bool IsRecordExecSql { get; set; }

    /// <summary>
    /// 启用库表差异日志
    /// </summary>
    public bool EnableDiffLog { get; set; }

    /// <summary>
    /// 主键不自增的数据库表名
    /// </summary>
    public List<string> NoIsIdentityDbTables { get; set; }

    /// <summary>
    /// 数据库配置集合
    /// </summary>
    public List<SqlSugar.ConnectionConfig> ConnectionConfigs { get; set; }

    public void OnListener(SqlSugarDbSettingOptions options, IConfiguration configuration)
    {
        //Name = options.Name;  // 实时的最新值
        //Version = options.Version;  // 实时的最新值
        //Company = options.Company;//公司

        Type entityType = GetType();//获得该类的Type

        var dics = options.GetDictionary();
        foreach (var item in dics)
        {
            PropertyInfo? propertyInfo = entityType.GetProperty(item.Key);
            propertyInfo?.FieldSetValue(item.Key, item.Value as object);
        }
    }



    public void PostConfigure(SqlSugarDbSettingOptions options, IConfiguration configuration)
    {
    }

}